import processing.core.*; 
import processing.xml.*; 

import java.applet.*; 
import java.awt.*; 
import java.awt.image.*; 
import java.awt.event.*; 
import java.io.*; 
import java.net.*; 
import java.text.*; 
import java.util.*; 
import java.util.zip.*; 
import java.util.regex.*; 

public class _15a_BoucleObjetXMLTypoPixelTabColis extends PApplet {

XMLElement xml;
String[] t_tag;
int[] t_count;
int v_convertit;
Fonte[] t_o_tag;
PFont maFonte;


public void setup(){
  size(900, 600);
  frameRate(3);

  maFonte = createFont("data/AGaramondPro-Regular.otf", 50);
  xml = new XMLElement(this, "data/tagmehdi.xml"); // appel du fichier XML
  int v_nbrPost = xml.getChildCount();
  t_tag = new String[0];
  t_count = new int[0];

  for (int i = 0; i < v_nbrPost; i++) {
    XMLElement v_post = xml.getChild(i); // Initialise un objet v_post qui correspond au i-eme enfant du noeud XML

    v_convertit =  PApplet.parseInt(v_post.getStringAttribute("count"));

    if(v_convertit > 7){
      t_tag = append(t_tag,v_post.getStringAttribute("tag"));
      t_count = append(t_count,v_convertit);
    }
  }

  t_o_tag = new Fonte[t_tag.length];

  for(int i = 0; i < t_o_tag.length; i++){
    t_o_tag[i] = new Fonte(t_count[i],t_tag[i]);
    t_o_tag[i].ecrit();
  }
  //noLoop();
}

public void draw(){
  //background(172);
  for(int i = 0; i < t_o_tag.length; i++){
    t_o_tag[i].bouge();
  }
}





class Fonte{


  float v_corps;
  String v_mot;
  float x;
  float y;
  Fonte(float p_corps, String p_mot){
    v_corps = p_corps;
    v_mot = p_mot;
    //maFonte = createFont("data/AGaramondPro-Regular.otf", 5*v_corps-16);
  }

  public void ecrit() {
    fill(0,random(50,90));    

    textFont(maFonte);

    float v_largeur = textWidth(v_mot);
    float x = random(width-v_largeur);
    float y = random(1.5f*v_corps,height-1.5f*v_corps);
    text(v_mot, x, y);
  }

  public void bouge() {
    float v_largeur = textWidth(v_mot);
    
    float centreX = (width/2)-(v_largeur/2);
    float centreY = (height/2)+(v_corps/2);

    float x = centreX+random(-0.9f*centreX,0.9f*centreX);
    float y = centreY+random(-0.85f*centreY,0.85f*centreY);
    

    text(v_mot, x, y);
    /*println(x);
     println(y);
     println("---------------------");*/
  }
}






  static public void main(String args[]) {
    PApplet.main(new String[] { "--bgcolor=#ffffff", "_15a_BoucleObjetXMLTypoPixelTabColis" });
  }
}
